package com.xzc.web;

import com.xzc.domain.User;
import com.xzc.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 徐州城
 * @version 1.0
 */
public class LoginServlet extends HttpServlet {
    //创建服务层
    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //2、调用 userService.login() 完成业务要求
        User user = userService.loginUser(username, password);

        //如果user等于null说明登录失败
        if (user == null) {
            //返回登录页面
            req.getRequestDispatcher("/pages/user/login.html").forward(req,resp);
        } else {
            //登录成功，跳到 login_success.html页面
            req.getRequestDispatcher("/pages/user/login_success.html").forward(req,resp);
        }
    }
}
